﻿using Microsoft.Extensions.Logging;

namespace Devonline.Identity;

/// <summary>
/// 根据上下文获取 oauth 第三方认证数据
/// </summary>
/// <param name="logger"></param>
/// <param name="dataService"></param>
public class OAuthUserService(
    ILogger<OAuthUserService> logger,
    IDataService<OAuthUser> dataService)
{
    private readonly ILogger<OAuthUserService> _logger = logger;
    private readonly IDataService<OAuthUser> _dataService = dataService;

    /// <summary>
    /// 获取用户第三方认证信息
    /// </summary>
    /// <returns></returns>
    public async Task<OAuthUser?> GetAsync()
    {
        _logger.LogInformation($"{_dataService.UserName} 获取个人第三方认证信息!");
        var userId = _dataService.GetUserId();
        if (userId is null)
        {
            _logger.LogInformation("用户尚未登录, 暂未获取到个人第三方认证信息");
            return default;
        }

        return await _dataService.FirstOrDefaultAsync(x => x.Id == userId || x.UserId == userId || x.OpenId == userId);
    }
}